package com.yu.threadpool;

import java.util.concurrent.*;

public class ThreadPoolUntils {
    public static volatile ThreadPoolExecutor threadPool;

    /**
     * 无返回值直接执行
     * @param runnable
     */
    public  static void execute(Runnable runnable){
        getThreadPool().execute(runnable);
    }

    /**
     * 返回值直接执行
     * @param callable
     */
    public  static <T> Future<T> submit(Callable<T> callable){
        return   getThreadPool().submit(callable);
    }

    /**
     * 关闭线程池
     */
    public static void shutdown() {
        getThreadPool().shutdown();
    }
    /**
     * dcs获取线程池
     * @return 线程池对象
     */
    public static ThreadPoolExecutor getThreadPool() {
        if (threadPool == null) {
            synchronized (ThreadPoolUntils.class) {
                if (threadPool == null) {
                    threadPool = new ThreadPoolExecutor(16,
                            32,
                            60,
                            TimeUnit.SECONDS,
                            new LinkedBlockingQueue<>(200),
                            new ThreadPoolExecutor.CallerRunsPolicy());
                }
            }
        }
        return threadPool;
    }

}
